<template>
  <div class="app">
    GOODS_CATEGORIES
  </div>
</template>

<script>
import { getCategories } from '@/api/goods'
import { excel, pagination, query } from '@/mixins'
import { parseTime } from '@/utils'
// import importAll from '@/utils/importAll'
// const components =  importAll(require.context('./components', true, /\.vue$/))

// 导出 excel
const tHeader = ['用户名', '角色', '状态', '电话', '邮箱', '注册时间']
const filterVal = ['username', 'role_name', 'mg_state', 'mobile', 'email', 'create_time']
const excelName = '用户列表'

export default {
  name: 'GOODSCATEGORIES',
  // components: {
  //   ...components
  // },
  mixins: [pagination(), query(), excel(tHeader, filterVal, excelName, 'excelData')],
  data() {
    return {
      queryForm: {
        query: ''
      },
      tableData: [],
      dialogVisible: false,
      type: 1,
      userId: '',
      roleVisible: false,
      user: {},
      excelData: []
    }
  },
  created() {
    // this.loadTableData()
  },
  methods: {
    async loadTableData() {
      const params = {
        query: this.queryForm.query,
        pagenum: this.m_pagination.page,
        pagesize: this.m_pagination.pageSize
      }
      const { users, total } = await getCategories(params)
      this.tableData = users
      this.m_pagination.total = total
      this.getExcelData()
    },
    addUser() {
      this.type = 1
      this.dialogVisible = true
    },
    async deleteUser(user) {
      this.$confirm(`删除${user.username}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteUser(user.id)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.loadTableData()
      }).catch(() => {
        console.log('取消删除')
      })
    },
    editUser(id) {
      this.type = 2
      this.userId = id
      this.dialogVisible = true
    },
    async putUserState(user) {
      console.log('user:', user)
      await putUserState(user)
      this.$message({
        message: '更改成功',
        type: 'success'
      })
      this.loadTableData()
    },
    editUserRole(user) {
      this.user = user
      this.roleVisible = true
    },
    getExcelData() {
      this.excelData = this.tableData.map(i => {
        i.mg_state = i.mg_state ? '启用' : '禁用'
        i.create_time = parseTime(i.create_time)
        return i
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
